﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using System.IO;
namespace QUANLYDIEMHOCSINH
{
    public partial class XEM_IN_PHIEUDIEM : Form
    {
        HOCKYBUS hkbus = new HOCKYBUS();
        LOPBUS lopbus = new LOPBUS();
        DIEMBUS diembus = new DIEMBUS();
        HOCSINHBUS hsbus = new HOCSINHBUS();
        NAMHOCBUS nambus = new NAMHOCBUS();
        public XEM_IN_PHIEUDIEM()
        {
            InitializeComponent();
            set_AN();
            LoadDanhSachNamHoc();
            LoadDanhSachHocKy();
            LoadDanhSachLop();
            load_ds();
        }
        void set_AN()
        {
            if (File.Exists("AnhNen.txt"))
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr = new StreamReader(fs);
                string s = null;
                string t = null;
                while ((s = sr.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream("AnhNen.txt", FileMode.Append);
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine("Turquoise");
                sw.Flush();
                sw.Close();
                fs.Close();

                FileStream fs1 = new FileStream("AnhNen.txt", FileMode.Open);
                StreamReader sr1 = new StreamReader(fs1);
                string s = null;
                string t = null;
                while ((s = sr1.ReadLine()) != null)
                {
                    t = s;
                }
                this.BackColor = Color.FromName(t);
                fs1.Close();
            }
        }
        void LoadDiemHocSinh(int mahk, int mahs, int nam)
        {
            dtgrwDiemHocSinh.DataSource = diembus.LoadDiemHocSinh(mahk,mahs,nam);
        }

        void LoadDanhSachHocKy()
        {
            cbHocKy.DataSource = hkbus.LoadDachHocKy();
            cbHocKy.DisplayMember = "TENHK";
            cbHocKy.ValueMember = "MAHK";
            cbHocKy.SelectedIndex = 0;
        }

        void LoadDanhSachLop()
        {
            cbLop.DataSource = lopbus.layDSL();
            cbLop.DisplayMember = "TENLOP";
            cbLop.ValueMember = "MALOP";
            cbLop.SelectedItem = 0;
        }

        void LoadDanhSachNamHoc()
        {
            cbNam.DataSource = nambus.LoadDanhSachNamHoc();
            cbNam.DisplayMember = "NAMBATDAU";
            cbNam.ValueMember = "MANAMHOC";
        }
        void load_ds()
        {
            HOCSINHBUS hocsinhbus = new HOCSINHBUS();
            GridTTHS.DataSource = hocsinhbus.dslop(int.Parse(cbLop.SelectedValue.ToString()));
            for (int i = 0; i < GridTTHS.Rows.Count; i++)
            {
                GridTTHS.Rows[i].Cells["stt"].Value = (i + 1).ToString();
            }
            GridTTHS.Columns["MAHS"].Visible = false;
            GridTTHS.Columns[3].Visible = false;
            GridTTHS.Columns[4].Visible = false;
            GridTTHS.Columns[5].Visible = false;
            GridTTHS.Columns[6].Visible = false;
            GridTTHS.Columns[7].Visible = false;
            GridTTHS.Columns[8].Visible = false;
            GridTTHS.Refresh();
        }
        int mahs=0;
        string tenhs = "";
        private void GridTTHS_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                lblTen.Text = GridTTHS.Rows[e.RowIndex].Cells["TENHS"].Value.ToString();
                tenhs = GridTTHS.Rows[e.RowIndex].Cells["TENHS"].Value.ToString();
                mahs = int.Parse(GridTTHS.Rows[e.RowIndex].Cells["MAHS"].Value.ToString());
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count ; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            GridTTHS.DataSource = hsbus.TimhocSinh(txtTimKiem.Text);
        }

        private void cbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            load_ds();
        }

        private void cbHocKy_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mahs != 0)
            {
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count - 1; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        private void cbNam_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (mahs != 0)
            {
                LoadDiemHocSinh(int.Parse(cbHocKy.SelectedValue.ToString()), mahs, int.Parse(cbNam.SelectedValue.ToString()));
                double dtb = 0;
                for (int i = 0; i < dtgrwDiemHocSinh.Rows.Count - 1; i++)
                {
                    dtb += double.Parse(dtgrwDiemHocSinh.Rows[i].Cells["tbmon"].Value.ToString());
                }
                if (dtgrwDiemHocSinh.Rows.Count != 0)
                {
                    lblDiemTB.Text = string.Format("{0:0.0}", (dtb / dtgrwDiemHocSinh.Rows.Count));
                }
            }
        }

        

        private void btnInDiem_Click(object sender, EventArgs e)
        {
            if(mahs==0)
            {
                MessageBox.Show("Chưa Chọn Học Sinh!", "Thông Báo");
                return;
            }
            frmDiemHS frmDiem = new frmDiemHS();
            frmDiem.Namhoc= int.Parse(cbNam.SelectedValue.ToString());
            frmDiem.Hocky = int.Parse(cbHocKy.SelectedValue.ToString());
            frmDiem.Tenhs = tenhs;
            frmDiem.Mahocsinh = mahs;
            frmDiem.DiemTB = lblDiemTB.Text;
            frmDiem.ShowDialog();
        }

        private void GridTTHS_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
            GridTTHS.Rows[e.RowIndex].Cells["stt"].Value = e.RowIndex + 1;
        }
    }
}
